package controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;

import cn.johnyu.util.WebServiceUtil;
import po.Client;
import service.ClientService;
import service.OrdersService;
import vo.OrdersVo;
import vo.ResponseEntity;

@Controller
@RequestMapping("/client")
public class ClientController {
	
	@Autowired
	private ClientService clientService;
	@Autowired
	private OrdersService ordersService;
	
	@RequestMapping("/login")
	@ResponseBody
	public ResponseEntity login(String cname,String cpassword,HttpServletRequest request){
		ResponseEntity re = new ResponseEntity();
		Client c = clientService.login(cname, cpassword);
		if(c == null){
			re.setStatus(false);
			re.setMessage("username or password is wrong!");
		}else{
			re.setStatus(true);
			request.getSession().setAttribute("client", c);
		}
		return re;
	}
	
	@RequestMapping("/exists")
	@ResponseBody
	public ResponseEntity findByName(String cname){
		ResponseEntity re = new ResponseEntity();
		boolean flag = clientService.existClient(cname);
		if(flag){
			re.setStatus(true);
			re.setMessage("username is exist!!");
		}else{
			re.setStatus(false);
			re.setMessage("username can be used!!");
		}
		return re;
	}
	
	@RequestMapping("/regist")
	@ResponseBody
	public ResponseEntity regist(Client c){
		ResponseEntity re = new ResponseEntity();
		int count = clientService.regist(c);
		if(count == 1){
			re.setStatus(true);
			re.setMessage("regist success");
		}else{
			re.setStatus(false);
		}
		return re;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(Client c,HttpServletRequest request){
		
		ModelAndView mv = new ModelAndView();
		Client c2 = (Client)request.getSession().getAttribute("client");
		c.setCid(c2.getCid());		
		c.setCpassword(c2.getCpassword());
		boolean flag = clientService.updateClient(c);
		if(flag){
			request.getSession().setAttribute("client", c);
//			mv.addObject("mainPage","editInfo.jsp");
//			mv.setViewName("personInfo.jsp");
		}
		return "true";
	}
	
	@ResponseBody
	@RequestMapping("/getComplain")
	public List<OrdersVo> getComplain(HttpSession session) {
		Client client = (Client) session.getAttribute("client");
		//String url= Global.GET_COMPLAIN_URL + "/" + client.getCid();
		Map<String, Integer> map = WebServiceUtil.complainWebServiceUtil(client.getCid());
		List<OrdersVo> ordersVos = new ArrayList<OrdersVo>();
		if(map!=null){
			for(String s:map.keySet()){
				OrdersVo ordersVo=ordersService.findOrderVo(s);
				ordersVo.setCompainResult(map.get(s));
				ordersVos.add(ordersVo);
			}			
		}
		return ordersVos;

	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:/index";
	}
}
